/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
/**
 * 
 */
package org.myontology.users;

import java.util.List;

import org.myontology.controller.AbstractAction;
import org.myontology.controller.AttributeHolder;
import org.myontology.controller.AttributeLevel;
import org.myontology.controller.ValueHandler;
import org.openid4java.consumer.ConsumerManager;
import org.openid4java.discovery.DiscoveryInformation;
import org.openid4java.message.AuthRequest;
import org.openid4java.server.RealmVerifier;

/**
 *  @author <a href="michael.hackl@sti2.at">Michael Hackl</a>
 * Project: Myontology - www.myontology.org
 * STI
 */
public class OpenIDVerifier extends AbstractAction {

	public static final ValueHandler<ConsumerManager> CONSUMER_MANAGER = new ValueHandler<ConsumerManager>(AttributeLevel.SESSION,OpenIDVerifier.class);
	public static final ValueHandler<DiscoveryInformation> DISCOVERY_INFORMATION = new ValueHandler<DiscoveryInformation>(AttributeLevel.SESSION,OpenIDVerifier.class);
	public static final ValueHandler<AuthRequest> REQUEST = new ValueHandler<AuthRequest>(AttributeLevel.REQUEST,OpenIDVerifier.class);
	
	/* (non-Javadoc)
	 * @see org.myontology.controller.Action#execute(org.myontology.controller.AttributeHolder)
	 */
	public void execute(AttributeHolder attributes) throws Exception {
		try {
			ConsumerManager manager = new ConsumerManager();
			RealmVerifier rV = new RealmVerifier();
			rV.setEnforceRpId(false);
			manager.setRealmVerifier(rV);
			String returnURL = "http://myontology.deri.at/openid/login";
			String userURL = attributes.getRequestParameter("openid_url");
			List discoveries = manager.discover(userURL);
			DiscoveryInformation discovered = manager.associate(discoveries);
			DISCOVERY_INFORMATION.setValue(attributes,discovered);
			AuthRequest authReq = manager.authenticate(discovered, returnURL);
			REQUEST.setValue(attributes, authReq);
		} catch (Exception e) {
			
			
		}
		
	}

}
